unit SnoopTimer;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ExtCtrls;

type
  TSnoopTimer = class(TTimer)
  private
    function GetSnooze: Longint;
    procedure SetSnooze(const Value: Longint);
    { Private declarations }
  protected
    { Protected declarations }
  public
    { Public declarations }
    constructor Create(AOwner:TComponent);override;
    destructor Destroy;override;
    property Snooze:Longint read GetSnooze write SetSnooze;
    procedure AddHook();
    procedure RemoveHook();
  published
    { Published declarations }
  end;

implementation

var
  ElapsedTime:Longint;
  whKeyBoard,whMouse:HHook;

function MouseHookCallBack(Code:integer;Msg:lParam;MouseHook:wParam):DWORD;stdcall;
begin
  if Code>=0 then ElapsedTime :=GetTickCount;
  Result := CallNextHookEx(whMouse,Code,Msg,MouseHook);
end;

function KeyBoardCallBack(Code:integer;Msg:word;KeyBoardHook:Longint):LongInt;stdcall;
begin
  if Code>=0 then ElapsedTime :=GetTickCount;
  Result := CallNextHookEx(whKeyBoard,Code,Msg,KeyBoardHook);
end;

constructor TSnoopTimer.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
end;

procedure TSnoopTimer.AddHook();
begin
    ElapsedTime :=GetTickCount;
    whMouse := SetWindowsHookEx(WH_MOUSE,@MouseHookCallback,GetDesktopWindow,0);
    whKeyBoard :=SetWindowsHookEx(WH_KEYBOARD,@KeyBoardCallBack,GetDesktopWindow,0);
end;

procedure TSnoopTimer.RemoveHook();
begin
    UnhookWindowsHookEx(whKeyBoard);
    UnhookWindowsHookEx(whMouse);
end;

destructor TSnoopTimer.Destroy;
begin
  inherited;
end;

function TSnoopTimer.GetSnooze: Longint;
begin
  Result:= GetTickCount - ElapsedTime;
end;

procedure TSnoopTimer.SetSnooze(const Value: Longint);
begin
  ElapsedTime := GetTickCount + Value;
end;

end. 

